package com.assessment.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.assessment.entity.Arole;
import com.assessment.entity.Ascore;
import com.assessment.entity.Atype;
import com.assessment.entity.Column;
import com.assessment.service.AscoreService;
import com.assessment.service.AtypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.assessment.utils.Constant.*;
import static com.assessment.utils.Constant.DELETE_FAILED;
import static com.assessment.utils.ReturnMap.failedMap;
import static com.assessment.utils.ReturnMap.successMap;
import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@CrossOrigin("*")
@RestController
@Api(tags={"全部打分信息统计"})
@RequestMapping("/api/ascore")
public class AscoreApi {
    private static final Logger log = LoggerFactory.getLogger(AscoreApi.class);
    private final AtypeService atypeService;
    private final AscoreService service;

    public AscoreApi(AscoreService service, AtypeService atypeService) {
        this.service = service;
        this.atypeService = atypeService;
    }

    // 获取分页列表
    @GetMapping("/getList")
    public Map<String,Object> getList(@RequestParam("searchInfo") String item)
    {
        try {
            QueryWrapper<Ascore> stringQueryWrapper = new QueryWrapper<>();
            stringQueryWrapper.select("DISTINCT itname");

            QueryWrapper<Ascore> queryWrapper = new QueryWrapper<>();
            if (item != null) {
                JSONObject jsonObject = JSON.parseObject(item);
                if (!isEmpty(jsonObject.get("apid"))) {
                    queryWrapper.eq("apid", jsonObject.get("apid"));
                    stringQueryWrapper.eq("apid", jsonObject.get("apid"));
                }else{
                    return failedMap(TABLE_FAILED);
                }
            }else {
                return failedMap(TABLE_FAILED);
            }
            List<Ascore> list = service.list(queryWrapper);

            List<Ascore> ascoreList = service.list(stringQueryWrapper);
            ArrayList<String> strings = new ArrayList<>();
            for (Ascore ascore : ascoreList) {
                strings.add(ascore.getItname());
            }
            Map<String, Object> res = new HashMap<>();
            res.put("scoreList",list);
            res.put("itemList",strings);
            return successMap(TABLE_SUCCESS,res);
        }catch (Exception e){
            return failedMap(TABLE_FAILED);
        }
    }

    // 更新
    @PostMapping("/update")
    public Map<String, Object> updateById(@RequestBody Ascore item) {
        try {
            if (service.updateById(item)) return successMap(UPDATE_SUCCESS);
            return failedMap(UPDATE_FAILED);
        }catch (Exception e){
            e.printStackTrace();
            return failedMap(UPDATE_FAILED);
        }
    }
    // 更新
    @PostMapping("/updateBatch")
    public Map<String, Object> updateById(@RequestBody List<Ascore> item) {
        try {
            if (service.updateBatchById(item)) return successMap(UPDATE_SUCCESS);
            return failedMap(UPDATE_FAILED);
        }catch (Exception e){
            e.printStackTrace();
            return failedMap(UPDATE_FAILED);
        }
    }
}

